package com.itheima.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCDemo04 {
    public static void main(String[] args) throws Exception {
        //1. 注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2. 获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "root");
        //3. 获取语句执行体对象
        Statement st = conn.createStatement();
        //4. 编写sql
        String sql = "SELECT * FROM USER";
        //5. 执行sql
        ResultSet rs = st.executeQuery(sql);

        //处理结果集
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            int age = rs.getInt("age");
            System.out.println(id + "-" + name + "-" + age);
        }
        //6. 释放资源
        st.close();
        conn.close();
    }
}
